<?php

namespace app\admin\controller\distribution;

use app\admin\controller\AuthController;
use app\common\model\DistributionOrder;
use app\common\model as M;
use service\JsonService;
use service\PHPExcelService;
use think\Db;
use think\Log;

/**
 *  分销订单
 * Class SystemConfig
 * @package app\admin\controller\system
 */
class Order extends AuthController
{
    /**
     * 基础配置
     * */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 直销订单列表
     */
    public function getOrderList()
    {
        $user = $this->adminInfo;

        $builder = Db::table('eb_distribution_order o')
            ->field('o.id,o.relation_type,
    o.relation_name,o.actual_amount,o.order_amount as total_amount,
    (o.commission+o.p_commission+o.platform_fee) as commission,e.pay_type,e.order_type,e.create_time,s.school_name,du.nickname as spread_user,pdu.nickname as p_spread_user,org.organ_name')
            ->join("eb_order e", 'e.id = o.relation_id', 'left')
            ->join("eb_school s", 's.id=e.school_id', 'left')
            ->join("eb_organ org", 'org.id=o.org_id', 'left')
            ->join("eb_distribution_user duser", 'duser.user_id=o.user_id and duser.org_id=o.org_id', 'left')
            ->join('eb_user du', 'du.uid=duser.user_id', 'left')
            ->join('eb_user pdu', 'pdu.uid=duser.pid', 'left');
        if ($user['org_id']) {
            $builder->where('o.org_id', $user['org_id']);
        }
        if (input('school_id')) {
            $builder->where('e.school_id', input('school_id'));
        }
        if (input('date')) {
            $builder->where('e.create_time', 'like', input('date') . '%');
        }

        if(input('export')){
            $result = $builder->select();   
            foreach ($result as $k => $v) {
                $v['pay_type_display']   = $v['pay_type']?M\Order::$pay_type[$v['pay_type']]:"";
                $v['pay_type_display']   = $v['pay_type']?M\Order::$pay_type[$v['pay_type']]:"";
                $v['order_type_display'] = $v['order_type']?M\Order::$order_type[$v['order_type']]:"";
                $v['commission']         = round($v['commission'],2);
                $result[$k]              = $v;
            }
            $exportData=[];
            foreach ($result as $key => $value) { 
                $exportData[]=[$value['school_name'],$value['create_time'],$value['nickname'],$value['relation_name'],$value['pay_type_display'],$value['total_amount'],$value['actual_amount'],$value['spread_user'],$value['p_spread_user'],$value['commission']];
            }
            PHPExcelService::setExcelHeader([lang('School') . '名称','订单日期','用户昵称','购买服务/商品/套餐','付款方式','应收总额','实收总额','推广人','上级推广人','佣金支出'])
            ->setExcelTile('分销订单导出','订单信息',' 生成时间：'.date('Y-m-d H:i:s',time()))
            ->setExcelContent($exportData)->ExcelSave();
        }else{
            $result = $builder->order('o.id','desc')->paginate(10);        
            foreach ($result as $k => $v) {
                $v['reltion_type_display'] = M\DistributionSeparate::$reltion_type[$v['relation_type']] ?? '-';
                $v['pay_type_display']     = $v['pay_type']?M\Order::$pay_type[$v['pay_type']]:"";
                $v['pay_type_display']     = $v['pay_type']?M\Order::$pay_type[$v['pay_type']]:"";
                $v['order_type_display']   = $v['order_type']?M\Order::$order_type[$v['order_type']]:"";
                $v['commission']           = round($v['commission'],2);
                $result[$k]                = $v;
            }
            return JsonService::successful($result);
        }
    }

    /**
     * 获取index页面统计数据
     */
    public function getSumAmount()
    {
        $user = $this->adminInfo;

        $builder = Db::table('eb_distribution_order o')
            ->field('COALESCE(sum(o.actual_amount),0) as actual_amount,COALESCE(sum(o.commission+o.p_commission+o.platform_fee),0) as commission')
            // ->join("eb_order e", 'e.id = o.relation_id', 'left')
            // ->join("eb_school s", 's.id=e.school_id', 'left')
            ->join("eb_distribution_user duser", 'duser.id=o.user_id', 'left')
            ->join('eb_user du', 'du.uid=duser.user_id', 'left')
            ->join('eb_user pdu', 'pdu.uid=duser.pid', 'left');
        if ($user['org_id']) {
            $builder->where('o.org_id', $user['org_id']);
        }
        if (input('school_id')) {
            $builder->where('e.school_id', input('school_id'));
        }
        if (input('date')) {
            $builder->where('e.create_time', 'like', input('date') . '%');
        }

        $result=$builder->find();
        $result['commission']=round($result['commission'],2);
        $result['actual_amount']=round($result['actual_amount'],2);
        return JsonService::successful($result);
    }
}
